package vista;

import bd.MySQL;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import ctrl.Archivo;
import ctrl.Ctrl;
import ctrl.Monitoreo;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DebugGraphics;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class Principal extends javax.swing.JFrame {

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JPanel panelPrincipal;
	private JTextArea textArea;
	private JMenuItem menuConfig;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JMenuItem limpiarMenu;
	private JLabel contador;
	private JLabel correosEnviados;
	private JButton botonEmpezar;
	private JLabel statusLabel;
	private JScrollPane scrollPane;
	private JMenuItem menuSalir;
	private JSeparator jSeparator1;
	private JMenu jMenu1;
	private JMenuBar barraMenu;
	private JLabel jLabel1;
	private Monitoreo monitoreo;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Principal inst = new Principal();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public Principal() {
		super();
		initGUI();
		MySQL.registrarDriver();
		String error = Archivo.cargarCongif();
		if (error != null && !error.equals("OK")) {
			JOptionPane.showMessageDialog(this, error, "Error!",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("SAM Broadcaster Monitor (SBM)");
			{
				panelPrincipal = new JPanel();
				getContentPane().add(panelPrincipal, "Center");
				panelPrincipal.setLayout(null);
				panelPrincipal.setBounds(0, 0, 580, 337);
				{
					jLabel3 = new JLabel();
					panelPrincipal.add(jLabel3, new AnchorConstraint(188, 937,
							229, 799, AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					jLabel3.setText("Correos Enviados");
					jLabel3.setBounds(22, 287, 107, 14);
					jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
				}
				{
					botonEmpezar = new JButton();
					panelPrincipal.add(botonEmpezar, new AnchorConstraint(906,
							785, 974, 638, AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					botonEmpezar.setText("Monitorear");
					botonEmpezar.setBounds(199, 307, 85, 23);
					botonEmpezar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							botonEmpezarActionPerformed(evt);
						}
					});
				}
				{
					statusLabel = new JLabel();
					panelPrincipal.add(statusLabel, new AnchorConstraint(927,
							981, 974, 931, AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					statusLabel.setIcon(new ImageIcon(getClass()
							.getClassLoader().getResource(
									"images/16-circle-blue.png")));
					statusLabel.setBounds(444, 311, 29, 16);
					statusLabel.setHorizontalAlignment(SwingConstants.CENTER);
				}
				{
					jLabel2 = new JLabel();
					panelPrincipal.add(jLabel2, new AnchorConstraint(927, 912,
							974, 831, AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					jLabel2.setText("Estado:");
					jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
					jLabel2.setBounds(397, 311, 47, 16);
				}
				{
					scrollPane = new JScrollPane();
					panelPrincipal.add(scrollPane, new AnchorConstraint(228,
							740, 850, 38, AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					scrollPane.getVerticalScrollBar().setDebugGraphicsOptions(
							DebugGraphics.BUFFERED_OPTION);
					scrollPane
							.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
					scrollPane.setBounds(22, 76, 445, 210);
					{
						textArea = new JTextArea();
						scrollPane.setViewportView(textArea);
						textArea.setText("Console");
						textArea.setEditable(false);
					}
				}
				{
					jLabel1 = new JLabel();
					panelPrincipal.add(jLabel1, new AnchorConstraint(79, 885,
							185, 106, AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					jLabel1.setText("SAM Broadcaster Monitor (SBM)");
					jLabel1.setFont(new java.awt.Font("Cambria", 1, 28));
					jLabel1.setBounds(15, 11, 452, 36);
					jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
				}
				{
					correosEnviados = new JLabel();
					panelPrincipal.add(correosEnviados);
					correosEnviados.setText("10");
					correosEnviados.setBounds(139, 287, 37, 14);
				}
				{
					contador = new JLabel();
					panelPrincipal.add(contador);
					contador.setText("20");
					contador.setBounds(439, 287, 28, 14);
					contador.setHorizontalAlignment(SwingConstants.RIGHT);
				}
			}
			{
				barraMenu = new JMenuBar();
				setJMenuBar(barraMenu);
				{
					jMenu1 = new JMenu();
					barraMenu.add(jMenu1);
					jMenu1.setText("Menu");
					{
						menuConfig = new JMenuItem();
						jMenu1.add(menuConfig);
						menuConfig.setText("Configurar");
						menuConfig.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								menuConfigActionPerformed(evt);
							}
						});
					}
					{
						limpiarMenu = new JMenuItem();
						jMenu1.add(limpiarMenu);
						limpiarMenu.setText("Limpiar");
						limpiarMenu.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								limpiarMenuActionPerformed(evt);
							}
						});
					}
					{
						jSeparator1 = new JSeparator();
						jMenu1.add(jSeparator1);
					}
					{
						menuSalir = new JMenuItem();
						jMenu1.add(menuSalir);
						menuSalir.setText("Salir");
					}
				}
			}
			pack();
			this.setSize(517, 396);
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

	private void menuConfigActionPerformed(ActionEvent evt) {
		ConfigWindow config = new ConfigWindow(this);
		this.setEnabled(false);
		config.setVisible(true);
	}

	public void limpiarMenuActionPerformed(ActionEvent evt) {
		textArea.setText("");
		correosEnviados.setText("0");
		contador.setText(Ctrl.getTiempoEsperaMySQL());
	}

	private void botonEmpezarActionPerformed(ActionEvent evt) {
		if (botonEmpezar.getText().equals("Monitorear")) {
			botonEmpezar.setText("Parar");
			// Monitoreo debe ser una clase que extienda de Runnable.
			monitoreo = new Monitoreo(this);
			monitoreo.start();
		} else {
			monitoreo.setIsRunning(false);
			cambiarIcono('b');
			botonEmpezar.setText("Monitorear");
		}
	}

	public void mostrarMensaje(String mensaje, char error) {
		if (error == 'E') {
			JOptionPane.showMessageDialog(this, mensaje, "Error!",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	public void aumentarContadorCorreo() {
		contador.setText("" + (Integer.parseInt(contador.getText()) + 1));
	}

	public void cambiarTextoConsola(ArrayList<String> colaActual) {
		String texto = "";
		for (String s : colaActual) {
			texto += s + "\n";
		}
		textArea.setText(texto);
	}

	public void cambiarContador(int tiempoTemp) {
		contador.setText("" + tiempoTemp);

	}

	public void cambiarIcono(char c) {
		if (c == 'v')
			statusLabel.setIcon(new ImageIcon(getClass().getClassLoader()
					.getResource("images/16-circle-green.png")));
		
		if(c=='a')
			statusLabel.setIcon(new ImageIcon(getClass()
					.getClassLoader().getResource(
							"images/16-circle-orange.png")));
		if(c=='b')
			statusLabel.setIcon(new ImageIcon(getClass()
					.getClassLoader().getResource(
					"images/16-circle-blue.png")));
	}

	public int getContadorCorreo() {
		return Integer.parseInt(contador.getText());
	}

}
